//
//  PicPickerCollectionView.swift
//  sina_yl
//
//  Created by 袁量 on 2017/5/24.
//  Copyright © 2017年 袁量. All rights reserved.
//

import UIKit
private let picPickerCellID = "picPickerCellID"
private let edgeMargin : CGFloat = 10
private let itemMargin = 10
class PicPickerCollectionView: UICollectionView {

    var images : [UIImage] = [UIImage]() {
        didSet {
            reloadData()
        }
    }

    override func awakeFromNib() {
        super.awakeFromNib()
        let layout = collectionViewLayout as! UICollectionViewFlowLayout
        let itemWH = (UIScreen.main.bounds.size.width - 4 * edgeMargin) / 3
        layout.itemSize = CGSize(width: itemWH, height: itemWH)
        layout.minimumLineSpacing = edgeMargin
        layout.minimumInteritemSpacing = edgeMargin / 2
        contentInset = UIEdgeInsetsMake(edgeMargin, edgeMargin, 0, edgeMargin)
        dataSource = self
//        register(UICollectionViewCell.classForCoder(), forCellWithReuseIdentifier: picPickerCellID)
        register(UINib.init(nibName: "PicPickerViewCell", bundle: nil), forCellWithReuseIdentifier: picPickerCellID)
    }
}
extension PicPickerCollectionView : UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        print("点击过后增加了图片 \(images.count)")
        return images.count + 1
        
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        // 1.创建cell
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: picPickerCellID, for: indexPath) as! PicPickerViewCell
        // 2.给cell设置数据
//        cell.backgroundColor = UIColor.red
        cell.image = indexPath.item <= images.count - 1 ? images[indexPath.item] : nil
        
        return cell
    }
}


